package me.zhengjie.org.teamemployee.service.impl;

import me.zhengjie.org.teamemployee.domain.TeamEmployee;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.org.teamemployee.repository.TeamEmployeeRepository;
import me.zhengjie.org.teamemployee.service.TeamEmployeeService;
import me.zhengjie.org.teamemployee.service.dto.TeamEmployeeDto;
import me.zhengjie.org.teamemployee.service.dto.TeamEmployeeQueryCriteria;
import me.zhengjie.org.teamemployee.service.mapper.TeamEmployeeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "teamEmployee")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TeamEmployeeServiceImpl implements TeamEmployeeService {

    private final TeamEmployeeRepository teamEmployeeRepository;

    private final TeamEmployeeMapper teamEmployeeMapper;

    public TeamEmployeeServiceImpl(TeamEmployeeRepository teamEmployeeRepository, TeamEmployeeMapper teamEmployeeMapper) {
        this.teamEmployeeRepository = teamEmployeeRepository;
        this.teamEmployeeMapper = teamEmployeeMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(TeamEmployeeQueryCriteria criteria, Pageable pageable){
        Page<TeamEmployee> page = teamEmployeeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(teamEmployeeMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<TeamEmployeeDto> queryAll(TeamEmployeeQueryCriteria criteria){
        return teamEmployeeMapper.toDto(teamEmployeeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public TeamEmployeeDto findById(Integer id) {
        TeamEmployee teamEmployee = teamEmployeeRepository.findById(id).orElseGet(TeamEmployee::new);
        ValidationUtil.isNull(teamEmployee.getId(),"TeamEmployee","id",id);
        return teamEmployeeMapper.toDto(teamEmployee);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public TeamEmployeeDto create(TeamEmployee resources) {
        return teamEmployeeMapper.toDto(teamEmployeeRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(TeamEmployee resources) {
        TeamEmployee teamEmployee = teamEmployeeRepository.findById(resources.getId()).orElseGet(TeamEmployee::new);
        ValidationUtil.isNull( teamEmployee.getId(),"TeamEmployee","id",resources.getId());
        teamEmployee.copy(resources);
        teamEmployeeRepository.save(teamEmployee);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            teamEmployeeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<TeamEmployeeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TeamEmployeeDto teamEmployee : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("人员", teamEmployee.getEmpId());
            map.put("组织", teamEmployee.getTeamId());
            map.put("开始时间", teamEmployee.getStartTime());
            map.put("结束时间", teamEmployee.getEndTime());
            map.put("变动说明", teamEmployee.getDescribe());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}